﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.IO;
using System.Text;

using OptusMobile.Web.ServerComponents.BAL.HandsetManagement;
using OptusMobile.Web.ServerComponents.BAL.PlanManagement;
using OptusMobile.Web.ServerComponents.DAL;
using OptusMobile.Web.css;



namespace OptusMobile.Web.Handset
{
    public partial class AllHandsetShow : System.Web.UI.Page
    {
        private HandsetManager handsetManager;
        private PlanManager planManager;

        public AllHandsetShow()
        {
            handsetManager = new HandsetManager();
            planManager = new PlanManager();
           
           
        }


        
        protected void Page_Load(object sender, EventArgs e)
        {
            string cssPath = "../css/ShowAllHandset.css";

            HtmlLink link = new HtmlLink();
            link.Href = cssPath;
            link.Attributes["rel"] = "stylesheet";
            link.Attributes["type"] = "text/css";
            PlaceHolder1.Controls.Add(link);
            
            ShowAllHandset();
            Session["detaileCurImage"] = 0;

        }

        private string GetPlanName(string handsetID)
        {
            var planname = planManager.ReadAllCapDetails().Where(p => p.handsetID == handsetID).First();
            return planname.planName.ToString();
        }

        private void OnClick_Image(Object sender, CommandEventArgs e)
        {

            InsertTempPopularHandset(e.CommandArgument.ToString ());
            Response.Redirect("ShowHandset.aspx?imageID=" + e.CommandName.ToString () + "&handsetID=" + e.CommandArgument.ToString ());
        }

        private void OnClick_BuyButton(Object sender, CommandEventArgs e)
        {
            InsertTempPopularHandset(e.CommandArgument.ToString());
            Response.Redirect("BuyHandset.aspx?handsetID=" +e.CommandArgument.ToString ()+"&imageurl="+e.CommandName.ToString());
        }

        private void OnClick_AllPlanButton(Object sender, CommandEventArgs e)
        {
            InsertTempPopularHandset(e.CommandArgument.ToString());
            Response.Redirect("ShowHandsetDetail.aspx?handsetID=" + e.CommandArgument.ToString());
        }

        private void InsertTempPopularHandset(string HandsetID)
        {
            handsetManager.UpdateHandsetClickNumber(HandsetID);
        }

        private void ShowAllHandset()
        {

            HtmlGenericControl dynamicControl = new HtmlGenericControl();
            var result = handsetManager.GetAllHandsetDetailInformation();
            int handsetCount = result.Count;
            dynamicControl.Controls.Add(new LiteralControl("<div class=\"MainShowHandsetLayer\">"));
            

            for (int i = 0; i < handsetCount; i++)
            {
                StringBuilder strHtml = new StringBuilder();
                strHtml.Append("<div class=\"IndividualLayer\">");
                strHtml.Append("<table>");
                strHtml.Append("<tr>");
                strHtml.Append("<td id=\"tableTitel\" colspan=\"2\">");
                Label lbltitl = new Label();
                //string m = result[0].handsetCamera;
                lbltitl.ID = result[i].handsetID + i.ToString();
                lbltitl.Text = result[i].brandName.ToString() + result[i].handsetModole.ToString();
                //lbltitl.CssClass = "titlelbl";
                lbltitl.EnableViewState = true;

                LiteralControl lc1 = new LiteralControl(strHtml.ToString());
                lc1.EnableViewState = true;
                dynamicControl.Controls.Add(lc1);
                dynamicControl.Controls.Add(lbltitl);
                strHtml.Remove(0, strHtml.Length);
               
                strHtml.Append("</td>");
                strHtml.Append("</tr>");
                strHtml.Append("<tr>");
                strHtml.Append("<td id=\"HandsetPic\" rowspan=\"3\">");
                ImageButton imghandset = new ImageButton();
                imghandset.ID = result[i].imageID + i.ToString();
                imghandset.ImageUrl = ".."+result[i].imagePath + result[i].imageName;      //"../Image/Handset/lrg_104x221_BBerry_Curve8520_MAIN.png";
                imghandset.Command += new CommandEventHandler(this.OnClick_Image);
                imghandset.CommandArgument = lbltitl.ID.Substring(0, 16);
                imghandset.CommandName = imghandset.ID.Substring(0, 18);
                //imghandset.PostBackUrl = "ShowHandset.aspx?imageID=" + imghandset.ID.Substring(0, 18) + "&handsetID=" + lbltitl.ID.Substring(0, 16);
                imghandset.EnableViewState = true;
                


                LiteralControl lc2 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc2);
                dynamicControl.Controls.Add(imghandset);
                strHtml.Remove(0, strHtml.Length);


                strHtml.Append("</td>");
                strHtml.Append("<td id=\"bestoffer\">");
                Label lblbestoffer=new Label();
                lblbestoffer.ID = "bestoffer" + i.ToString();
                lblbestoffer.Text = GetPlanName(result[i].handsetID);
                lblbestoffer.EnableViewState = true;

                LiteralControl lc3 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc3);
                dynamicControl.Controls.Add(lblbestoffer);
                strHtml.Remove(0, strHtml.Length);
               

                strHtml.Append("</td>");
                strHtml.Append("</tr>");
                strHtml.Append("<tr>");
                strHtml.Append("<td id=\"arrow\">");
                Image imgbtbackground = new Image();
                imgbtbackground.ImageUrl = "/Image/Common/ad5.jpg";
                imgbtbackground.EnableViewState = true;

                LiteralControl lc4 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc4);
                dynamicControl.Controls.Add(imgbtbackground);
                strHtml.Remove(0, strHtml.Length);


                strHtml.Append("</td>");
                strHtml.Append("<tr>");
                strHtml.Append("<td>");
                strHtml.Append("<ul>");
                strHtml.Append("<li>");
                Label lblcamera = new Label();
                lblcamera.ID = "camera" + i.ToString();
                lblcamera.Text = result[i].handsetCamera;
                lblcamera.EnableViewState = true;

                LiteralControl lc5 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc5);
                dynamicControl.Controls.Add(lblcamera);
                strHtml.Remove(0, strHtml.Length);


                strHtml.Append("</li>");
                strHtml.Append("<li>");
                Label lblmemory = new Label();
                lblmemory.ID = "Memory" + i.ToString();
                lblmemory.Text = result[i].handsetMemory;
                lblmemory.EnableViewState = true;

                LiteralControl lc6 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc6);
                dynamicControl.Controls.Add(lblmemory);
                strHtml.Remove(0, strHtml.Length);


                strHtml.Append("</li>");
                strHtml.Append("<li>");
                Label lblsreensize = new Label();
                lblsreensize.ID = "sreensize" + i.ToString();
                lblsreensize.Text = result[i].handsetSreenSize;
                lblsreensize.EnableViewState = true;

                LiteralControl lc7 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc7);
                dynamicControl.Controls.Add(lblsreensize);
                strHtml.Remove(0, strHtml.Length);
                

                strHtml.Append("</li>");
                strHtml.Append("</ul>");
                strHtml.Append("</td>");
                strHtml.Append("</tr>");
                strHtml.Append("<tr>");
                strHtml.Append("<td>");
                //CheckBox chb = new CheckBox();
                //chb.ID="chk"+i.ToString();
                //chb.Text = "Compare";
                //chb.EnableViewState = true;

                LiteralControl lc8 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc8);
                //dynamicControl.Controls.Add(chb);
                strHtml.Remove(0, strHtml.Length);
               


                strHtml.Append("</td>");
                strHtml.Append("<td id=\"tdbutton\">");
                ImageButton imgbtbuy = new ImageButton();
                imgbtbuy.ID = "btbuy" + i.ToString();
                imgbtbuy.ImageUrl = "../Image/Common/buy_button_back.gif";
                //imgbtbuy.PostBackUrl = "../Handset/Buy.aspx?handsetID=" + lbltitl.ID.Substring(0, 16);
                imgbtbuy.Command += new CommandEventHandler(this.OnClick_BuyButton);
                imgbtbuy.CommandArgument = lbltitl.ID.Substring(0, 16);
                imgbtbuy.CommandName = imghandset.ImageUrl;
                imgbtbuy.EnableViewState = true;

                LiteralControl lc9 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc9);
                dynamicControl.Controls.Add(imgbtbuy);
                strHtml.Remove(0, strHtml.Length);
                

                ImageButton imgbtallplan = new ImageButton();
                imgbtallplan.ID = "Allplan" + i.ToString();
                imgbtallplan.Command += new CommandEventHandler(this.OnClick_AllPlanButton);
                imgbtallplan.CommandArgument = lbltitl.ID.Substring(0, 16);
                imgbtallplan.ImageUrl = "../Image/Common/allplans.png";
                //imgbtallplan.PostBackUrl = "/Handset/ShowHandsetDetail.aspx?handsetID=" + lbltitl.ID.Substring(0, 16);
                imgbtallplan.EnableViewState = true;

                LiteralControl lc10 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc10);
                dynamicControl.Controls.Add(imgbtallplan);
                strHtml.Remove(0, strHtml.Length);
  

                strHtml.Append("</td>");
                strHtml.Append("</tr>");
                strHtml.Append("</table>");
                strHtml.Append("</div>");

                LiteralControl lc11 = new LiteralControl(strHtml.ToString());
                dynamicControl.Controls.Add(lc11);


            }
            dynamicControl.Controls.Add(new LiteralControl("</div>"));
            PlaceHolder1.Controls.Add(dynamicControl);

        }
    }


}
